package top.lilele.adminSystem.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.lilele.adminSystem.mapper.FilePartMapper;

import javax.annotation.Resource;

/**
 * 上传异步操作
 *
 * @author lilele
 */
@Service
@Slf4j
public class AsyncUpload {
    @Resource
    MinioUtil minioUtil;

    @Resource
    FilePartMapper filePartMapper;

//    @Async("asyncTaskExecutor")
//    @SneakyThrows
//    public void mergePartAndUpload(String generateUploadId) {
//        List<Item> items = minioUtil.listObjects(generateUploadId);
//        QueryWrapper queryWrapper = new QueryWrapper();
//        queryWrapper.eq("uploadId", generateUploadId);
//        List<FilePartEntity> list = filePartMapper.selectList(queryWrapper);
//        Vector<InputStream> objects = new Vector<>();
//        List<InputStream> listInput = new ArrayList<>();
//        // 遍历list集合，拼接输出流
//        list.stream().forEach(item -> {
//            GetObjectResponse response;
//            try {
//                response = minioUtil.download(item.getFileName(), generateUploadId);
//                byte[] buf = new byte[1024];
//                int len;
//                try (FastByteArrayOutputStream os = new FastByteArrayOutputStream()) {
//                    while ((len = response.read(buf)) != -1) {
//                        os.write(buf, 0, len);
//                    }
//                    os.flush();
//                    byte[] bytes = os.toByteArray();
//                    ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
//                    listInput.add(inputStream);
////                    objects.add(inputStream);
//                }
//                log.info("response size {}", response.available());
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        });
//        //定义内存输出流
//        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//        //定义缓冲字节数组
//        byte[] array = new byte[1024];
//        //读取长度
//        int len = 0;
//        for (InputStream ips : listInput) {
//            while ((len = ips.read(array)) > 0) {
//                byteArrayOutputStream.write(array, 0, len);
//            }
//        }
//        InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
//        // 合并文件流 (SequenceInputStream 读出来的不准)
////        InputStream sequenceInputStream = new SequenceInputStream(objects.elements());
////        log.info("inputStream size {}", sequenceInputStream.available());
//        // 创建文件
//        String fileName = IdUtil.fastSimpleUUID();
//        // 上传合并之后的字节流
//        minioUtil.uploadStream(inputStream, fileName, bucketName);
//        // 删除桶下的所有临时对象
//        items.stream().forEach(item -> {
//            try {
//                minioUtil.deleteObj(list.get(0).getUploadId(), item.objectName());
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        });
//        //清除数据表临时数据
//        QueryWrapper queryWrapper1 = new QueryWrapper();
//        queryWrapper1.eq("uploadId", list.get(0).getUploadId());
//        filePartMapper.delete(queryWrapper1);
//        // 清除临时桶
//        minioUtil.removeBucket(list.get(0).getUploadId());
//        AdminFileEntity adminFileEntity = new AdminFileEntity();
//        adminFileEntity.setFileName(fileName);
//        adminFileEntity.setBucketName(bucketName);
//        adminFileEntity.setFileSize(10000L);
//        adminFileEntity.setFileExtension(".png");
//        adminFileMapper.insert(adminFileEntity);
//    }
}
